<?php

namespace App\Http\Controllers\Game\Data;

use App\Core\JWT;
use App\Services\Platform\SettlementService;
use Helper\Timed;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\View;
use App\Http\Controllers\Controller;
use App\Http\Resources\Common\DataResource;
use App\Http\Resources\Common\DatasResource;
use App\Services\Game\Data\Settlement\InfoService as SettlementInfoService;
use App\Models\Game\Settlement\Detail;
use App\Models\Game\Config\System as Config;
use App\Models\Game\Settlement\Info as SettlementInfo;

class SettlementController extends Controller
{

	public function __construct()
	{
		Timed::setForceNotAbs();
	}
    /**
     * 导出数据
     */
    public function export(Request $request)
    {
        SettlementInfoService::create()->getList($request, true);
    }


    /**
     * 获取数据列表
     */
    public function getList(Request $request)
    {
        list ($collection, $total, $stats) = SettlementInfoService::create()->getList($request);

        return DatasResource::create($collection)->setPaginateTotal($total)->setAttribute('stats', $stats);
    }

    /**
     * 获取详情链接
     */
    public function getDetail(Request $request)
    {
        $data = $request->validate([
            'sn' => 'required|string',
        ]);

        $token = JWT::instance()->getToken($data);

        $domain = Config::getDomain()['agency'];

        return new DataResource([
            'url' => "{$domain}/game/detail/{$token}?lang=" . App::getLocale()."&timezone=".$request->user()->config->timezone,
        ]);
    }

    /**
     * 游戏详情页面
     */
    public function detail(string $token, Request $request)
    {
	    if (!is_null($lang = $request->input('lang')) && in_array($lang, ['en', 'th', 'zh-CN', 'zh'])) {
		    if ($lang == 'zh') {
			    $lang = 'zh-CN';
		    }
		    App::setLocale($lang);
	    }

	    if (($params = JWT::instance()->parseToken($token)) === false) {
		    return abort(401);
	    }
	    if (is_null($sn = $params['sn'] ?? null)) {
		    return abort(404);
	    }

	    if (is_null($data = SettlementInfoService::create()->getDataBySn($sn))) {
		    return abort(404);
	    }

	    if (!View::exists("games.{$data['game_id']}")) {
		    return abort(404);
	    }

	    if (is_null($detail = Detail::getDetail($data['round_id']))) {
		    return 'ES : settlement_detail Round ID 数据未找到！';
	    }

	    // 游戏端语言
	    $languages = ['en' => 'en-US', 'th' => 'th-TH', 'zh-CN' => 'zh-CN', 'zh' => 'zh'];
	    $languages = ['en' => 'en-US', 'th' => 'th-TH', 'zh-CN' => 'zh-CN', 'zh' => 'zh-CN'];

	    if(isset($detail['details']['result']['bet_score']) && !empty($detail['details']['result']['bet_score'])){
		    $data['bet_base'] = $detail['details']['result']['bet_score'];
	    }

	    return view("games.{$data['game_id']}", [
		    'data' => $data,
		    'detail' => $detail,
		    'timezone' => $request->input('timezone', 'Asia/Shanghai'),
		    'domain' => Config::getDomain(),
		    'lang' => $languages[$lang],
	    ]);
    }
}
